<?php

/**
 * This Class is for you.
 * 
 * Feel free to edit this file to extend your models classes.
 * Please, never touch sufixed _protoweb classes
 */

 
/**
 * 
 *
 * @Authors Viera Leonel
 * @package mailing
 * @subpackage model
 *
 * 
 */
class Client extends Client_protoweb {

  public function __construct($id = NULL) {
    parent::__construct($id);
  } // end of method __construct($id = NULL)

  public function count_recipients() {
    $query = $this->db->query("SELECT count(*) as count FROM recipients WHERE recipients.client_id = {$this->id} AND recipients.offdate = 0 AND recipients.verified = 1");
    $res = $query->result();
    return $res[0]->count;
  }

  public function get_path() {
    $ci = get_instance();
    return $ci->config->item('clients_path') . $this->slug . '/';
  }

  public function current_campaigns() {
    return $this->campaign->where('sent',0);
  }

  public function clean_recipients() {

    set_time_limit(0);
    ini_set('memory_limit', '500M');

    // path to lock file
    $lock_file = "{$this->get_path()}cleaning";

    // check if lock file exists. If exists don't do anything
    if (file_exists($lock_file)) {
      return 'already_cleaning';
    }

    // create lock file
    if ( ! file_put_contents($lock_file, 'cleaning')) {
      return 'cant_create_lock_file';
    }

    // all permision
    chmod($lock_file, 0777);

    $recipients = new Recipient();
    $recipients->where('offdate',0)->where('verified',1)->where_related_client('id',$this->id)->get();

    // TODAY - 5 days    
    $limit_date = date('Y-m-d H:i:s',strtotime('-5 day',strtotime(date('Y-m-d H:i:s'))));
    /*
    $sends = new Send();
    $sends->where('cleaned',0)
          ->where('sent <',$limit_date)
          ->where_related_recipient('client_id',$this->id)
          ->where_related_recipient('verified',1)
          ->where_related_recipient('offreason',0)
          ->order_by('sent','asc')
          ->get();

    $bounces = 0;
    $recipient_id = 0;
    foreach ($sends as $send) {

      if ($recipient_id == 0) {
        $recipient_id = $send->recipient->id;
      }

      if ($recipient_id !== $send->recipient->id) {
        
        $recipient          = new Recipient($recipient_id);
        $recipient->bounces = $bounces;
        
        if (($recipient->bounces >= 3) AND ($recipient->offdate == 0)) {
          $recipient->offdate   = date('Y-m-d H:i:s');
          $recipient->offreason = 'Bounces';  
        }

        $recipient->save();
        $bounces = 0;
      }
      
      if ($send->bounced > 0) {
        $bounces++; 
      }
      else {
        if (($send->tries >= 2) AND ($send->awsmessageid == NULL)) {
          if ($send->recipient->offreason == 0 ) {
            $send->recipient->offreason = date('Y-m-d H:i:s');
            $send->recipient->offreason = 'SES tries';
            $send->recipient->save();
          }
        }
        else {
          $recipient->bounces = 0;
        } 
      }
      
      $send->cleaned = 1;
      $send->save();
      $recipient_id = $send->recipient->id;
    } // foreach $sends
    */
    
    // update every recipient stats
    foreach ($recipients as $recipient) {
      $sends = new Send();
      $sends->where_related_recipient('id',$recipient->id)->where('sent <',$limit_date)->where('cleaned',0)->order_by('sent','asc')->get();

      foreach ($sends as $send) {
        if ($send->bounced > 0) {
          $recipient->bounces++; 
          $send->cleaned = 1;
          $send->save();
        }
        else
          if ($send->tries >= 1 AND $send->awsmessageid == NULL) {
            if ($recipient->offreason = 0 ) {
              $recipient->offreason = $now;
              $recipient->offreason = 'SES tries';
            }
            $send->cleaned = 1;
            $send->save();
          }
          else {
            $recipient->bounces = 0;
            $send->cleaned = 1;
            $send->save();
          }
      } // foreach sends

      // if recipient has more than 2 bounces, it will now be off
      if ($recipient->bounces >= 3) {
        $recipient->offdate   = date('Y-m-d H:i:s');
        $recipient->offreason = 'Bounces';
      }
      $recipient->save();
    } // foreach recipients
    
    // delete lock file
    if (file_exists($lock_file)) {
      unlink($lock_file);
    }

    return 'cleaned';
  } // clean_recipients()

} // Class Client

/* End of file client.php */
/* Location: ./system/application/models/client.php */